/*
 * Copyright 2017 Baiyyy
 * @(#)PsZcdController.java
 */

package com.byyy.ccts.modules.cc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.byyy.ccts.core.common.controller.BaseCRUDController;
import com.byyy.ccts.core.model.AjaxJson;
import com.byyy.ccts.core.model.PageJson;
import com.byyy.ccts.core.query.data.PropertyPreFilterable;
import com.byyy.ccts.core.query.data.Queryable;
import com.byyy.ccts.core.query.utils.QueryableConvertUtils;
import com.byyy.ccts.core.query.wrapper.EntityWrapper;
import com.byyy.ccts.core.security.shiro.authz.annotation.RequiresPathPermission;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.cc.entity.ccts.*;
import com.byyy.ccts.modules.cc.service.*;
import com.byyy.ccts.modules.common.annotation.LogRecord;
import com.byyy.ccts.modules.sys.service.IParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Created by DELL on 2018-6-26.
 */
@Controller
@RequestMapping("${admin.url.prefix}/cc/zcd/mx")
@RequiresPathPermission("sys:user")
public class PsZcdMxController extends BaseCRUDController<PsZcdMx, String> {

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private ICouvenseService couvenseService;

    @Autowired
    private IChillCarService chillCarService;

    @Autowired
    private IPsZcdMxService psZcdMxService;

    @Autowired
    private IPsZcdService psZcdService;

    @Autowired
    private IParamService paramService;


    @Override
    public void preList(Model model, HttpServletRequest request, HttpServletResponse response) {
        String zhuangcdNo = request.getParameter("zhuangcdNo");
        PsZcdHz zcdHz = this.psZcdService.selectById(zhuangcdNo);
        Float electricAlarm = this.paramService.getFloatValueByCode("ELECTRIC_ALARM");
        Float electricWarning = this.paramService.getFloatValueByCode("ELECTRIC_WARNING");
        model.addAttribute("zcdHz", zcdHz);
        model.addAttribute("electricAlarm", electricAlarm.intValue());
        model.addAttribute("electricWarning", electricWarning.intValue());
    }

    @Override
    public void preAjaxList(Queryable queryable, EntityWrapper<PsZcdMx> entityWrapper, HttpServletRequest request,
                            HttpServletResponse response) {
        String zhuangcdNo = request.getParameter("zhuangcdNo");
        queryable.addCondition("zhuangcdNo", zhuangcdNo);
    }

    /**
     * 设备列表
     *
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "deviceList", method = RequestMethod.GET)
    public String deviceList(Model model, HttpServletRequest request, HttpServletResponse response) {
        String zhuangcdNos = request.getParameter("zhuangcdNos");
        model.addAttribute("zhuangcdNos", zhuangcdNos);
        return display("deviceList");
    }

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "deviceAjaxList", method = {RequestMethod.GET, RequestMethod.POST})
    public void deviceAjaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                               HttpServletResponse response) throws IOException {
        EntityWrapper<TADevice> entityWrapper = new EntityWrapper<TADevice>(TADevice.class);
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, TADevice.class);
        SerializeFilter filter = propertyPreFilterable.constructFilter(TADevice.class);
        String zhuangcdNo = request.getParameter("zhuangcdNos");
        PageJson<TADevice> pagejson = new PageJson<TADevice>(this.deviceService.deviceListForZcdMx(queryable, entityWrapper, Arrays.asList(zhuangcdNo.split(","))));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

    /**
     * 冷藏箱列表
     *
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "couvenseList", method = RequestMethod.GET)
    public String couvenseList(Model model, HttpServletRequest request, HttpServletResponse response) {
        String zhuangcdNo = request.getParameter("zhuangcdNo");
        model.addAttribute("zhuangcdNo", zhuangcdNo);
        return display("couvenseList");
    }

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "couvenseAjaxList", method = {RequestMethod.GET, RequestMethod.POST})
    public void couvenseAjaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                                 HttpServletResponse response) throws IOException {
        EntityWrapper<TACouvense> entityWrapper = new EntityWrapper<TACouvense>(TACouvense.class);
        String zhuangcdNo = request.getParameter("zhuangcdNo");
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, TACouvense.class);
        SerializeFilter filter = propertyPreFilterable.constructFilter(TACouvense.class);
        PageJson<TACouvense> pagejson = new PageJson<TACouvense>(this.couvenseService.couvenseListForZcdMx(queryable, entityWrapper, zhuangcdNo));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

    /**
     * 冷藏车列表
     *
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "chillCarList", method = RequestMethod.GET)
    public String chillCarList(Model model, HttpServletRequest request, HttpServletResponse response) {
        String zhuangcdNos = request.getParameter("zhuangcdNos");
        model.addAttribute("zhuangcdNos", zhuangcdNos);
        return display("chillCarList");
    }

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "chillCarAjaxList", method = {RequestMethod.GET, RequestMethod.POST})
    public void chillCarAjaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                                 HttpServletResponse response) throws IOException {
        EntityWrapper<TAChillCar> entityWrapper = new EntityWrapper<TAChillCar>(TAChillCar.class);
        String zhuangcdNos = request.getParameter("zhuangcdNos");
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, TAChillCar.class);
        SerializeFilter filter = propertyPreFilterable.constructFilter(TAChillCar.class);
        PageJson<TAChillCar> pagejson = new PageJson<TAChillCar>(this.chillCarService.chillCarListForZcdMx(queryable, entityWrapper, Arrays.asList(zhuangcdNos.split(","))));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

    /**
     * 装车单明细关联操作
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "relation", method = {RequestMethod.POST})
    @ResponseBody
    @LogRecord(title = "装箱关联")
    public AjaxJson relation(@RequestBody List<TsRelation> relations, BindingResult result,
                             HttpServletRequest request, HttpServletResponse response) throws IOException {
        AjaxJson ajaxJson = new AjaxJson();
        String msg = this.psZcdMxService.relation(relations);
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
            return ajaxJson;
        }
        ajaxJson.success("关联成功");
        return ajaxJson;
    }

    /**
     * 装车单明细列表
     *
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "zcdmxList", method = RequestMethod.GET)
    public String zcdmxList(Model model, HttpServletRequest request, HttpServletResponse response) {
        return display("../../zcdmx/list");
    }

    /**
     * 商品温度修改
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "updProdsTemp", method = {RequestMethod.POST})
    @ResponseBody
    @LogRecord(title = "商品温度更新")
    public AjaxJson updProdsTemp(@RequestBody List<PsZcdMx> zcdMxList, BindingResult result,
                                 HttpServletRequest request, HttpServletResponse response) throws IOException {
        AjaxJson ajaxJson = new AjaxJson();
        String msg = this.psZcdMxService.updProdsTemp(zcdMxList);
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
            return ajaxJson;
        }
        ajaxJson.success("商品温度更新完毕");
        return ajaxJson;
    }

    /**
     * 第三方单据温度修改
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "updProdsTempDSF", method = {RequestMethod.POST})
    @ResponseBody
    @LogRecord(title = "第三方单据温度修改")
    public AjaxJson updProdsTempDSF(@RequestBody List<PsZcdMx> zcdMxList, BindingResult result,
                                 HttpServletRequest request, HttpServletResponse response) throws IOException {
        AjaxJson ajaxJson = new AjaxJson();
        String msg = this.psZcdMxService.updProdsTempDSF(zcdMxList);
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
            return ajaxJson;
        }
        ajaxJson.success("第三方单据温度更新完毕");
        return ajaxJson;
    }

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "zcdMxAjaxList", method = {RequestMethod.GET, RequestMethod.POST})
    public void zcdMxAjaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                              HttpServletResponse response) throws IOException {
        EntityWrapper<PsZcdMx> entityWrapper = new EntityWrapper<PsZcdMx>(entityClass);
        preAjaxList(queryable, entityWrapper, request, response);
        propertyPreFilterable.addQueryProperty("id");
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass);
        SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
        PageJson<PsZcdMx> pagejson = new PageJson<PsZcdMx>(this.psZcdMxService.zcdMxList(queryable, entityWrapper));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

    /**
     * 装车单拆分个数页面
     *
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "orderSplitNumber", method = RequestMethod.GET)
    public String orderSplitNumber(Model model, HttpServletRequest request, HttpServletResponse response) {
        String zhuangcdId = request.getParameter("zhuangcdId");
        String shijJs = request.getParameter("shijJs");
        model.addAttribute("zhuangcdId", zhuangcdId);
        model.addAttribute("shijJs", shijJs);
        return display("orderSplitNumber");
    }

    /**
     * 装车单拆分
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "zcdSplit", method = {RequestMethod.POST})
    @ResponseBody
    @LogRecord(title = "装车单拆分")
    public AjaxJson zcdSplit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AjaxJson ajaxJson = new AjaxJson();
        String zhuangcdId = request.getParameter("zhuangcdId");
        int number = Integer.parseInt(request.getParameter("number"));

        String msg = this.psZcdMxService.zcdSplit(zhuangcdId, number);
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
            return ajaxJson;
        }
        ajaxJson.success("拆分成功");
        return ajaxJson;
    }

    /**
     * 托运装车单冷链查询
     * @return
     */
    @RequestMapping(value="zcdmxConsList", method = RequestMethod.GET)
    public String zcdDsf() {
        return display("../../zcdmx/listCons");
    }

    @RequestMapping(value="zcdmxNotConfirmList", method = RequestMethod.GET)
    public String zcdmxNotConfirmList(Model model, HttpServletRequest request, HttpServletResponse response) {
        String zhuangcdNo = request.getParameter("zhuangcdNo");
        PsZcdHz zcdHz = this.psZcdService.selectById(zhuangcdNo);
        model.addAttribute("zcdHz", zcdHz);
        return display("listNotConfirm");
    }

}
